package org.jeecg.modules.utils;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: jeecg-boot
 * @description: 网络请求
 * @author: wei
 * @create: 2022-11-21 17:01
 **/
@Slf4j
public class NetworkRequest {

//    String faviconUrl = getContentTest("https://sgdygb.blog.csdn.net/");
//        System.out.println("faviconUrl = " + faviconUrl);

    /**
     * @param str:
     * @return java.lang.String
     * @author wei
     * @description 网络请求
     * @date 2022/11/21 17:03
     */
    public static Map<String, String> getContentUrl(String str) {
        try {
            // 去掉多余部分，保留域名部分
            int i = StrUtil.indexOf(str, '/', StrUtil.indexOf(str, '.'));
            String url = StrUtil.sub(str, 0, i);

            // 假设输入的参数不携带协议，则拼接协议
            if (!isHttpOrHttps(url)) {
                url = StrUtil.format("https://{}", url);
            }

            // 发请求
            String userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36";
            Document document = Jsoup.connect(url).userAgent(userAgent).get();
            // 筛选包含favicon图标的link标签
            Elements title = document.select("link[type=image/x-icon]");
            title = ObjectUtil.isEmpty(title) ? document.select("link[rel$=icon]") : title;
            // 获取标题
            Elements nameDoc = document.select("title");
            String nameStr = nameDoc.text();

            // 获取favicon路径
            String href = title.attr("href");
            // 假设获取到的favicon路径已经包含了域名，则直接返回
            Map<String, String> map = new HashMap<>();
            map.put("name", nameStr);
            if (isHttpOrHttps(href) && StrUtil.containsAny(href, "favicon")) {
                map.put("href", href);
                return map;
            }
            href = StrUtil.format("{}/{}", url, StrUtil.removePrefix(href, "/"));
            map.put("href", href);
            // 拼接favicon的访问链接
            return map;
        } catch (IOException i) {
            i.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
//            return "链接无法识别";
            return null;
        }
        return null;
    }


    /**
     * 检测是否http或https
     *
     * @param url URL
     * @return 检验是否是http或https
     */
    public static boolean isHttpOrHttps(String url) {
        String lowerCaseStr = url.toLowerCase();
        return lowerCaseStr.startsWith("http:") || lowerCaseStr.startsWith("https:");
    }

}
